<?
class NewsController extends BaseController{
    var $table = 'news';
    var $id_field = 'new_id';
    var $name_field = 'new_title';
    var $thumb_field = 'new_picture';
    var $record_id;
    public function __construct($id = null,$seo_type = ''){
        parent::__construct($id);
        if($id) {
            $news = $this->getDataField();
            $db_cat = new db_query('SELECT cat_id,cat_name,cat_type FROM categories_multi WHERE cat_id = '.$news['new_cat_id'].' LIMIT 1');
            $cat = mysql_fetch_assoc($db_cat->result); unset($db_cat);
            if($cat['cat_type'] == 'recruitment') {
                $link = generate_recruitment_detail_url(array('new_id'=>$news['new_id'],'new_title'=>$news['new_title']));
            }
            elseif($cat['cat_type'] == 'promotion') {
                $link = generate_promotion_detail_url(array('new_id'=>$news['new_id'],'new_title'=>$news['new_title']));
            }
            elseif($cat['cat_type'] == 'technology') {
                $link = generate_technology_detail_url(array('new_id'=>$news['new_id'],'new_title'=>$news['new_title']));
            }
            else {
                $link = generate_news_detail_url(array('new_id'=>$news['new_id'],'new_title'=>$news['new_title'],'cat_name'=>$cat['cat_name']));
            }
            $seo_mt = showSeoMeta('news',$id);
                if($seo_mt != '') {
                    $im_seo = $seo_mt;
                }else {
                    $im_seo = '<title>'. $news['new_title'] .'</title>';
                    $im_seo .= '<meta name="description" content="'. cut_string(removeHTML($news['new_detail']),150) .'"/>';
            }
            $this->seo_string = ''.$im_seo.'
                                 <meta name="keywords" content="'.$news['new_tags'].'" />
                                 <meta property="og:site_name" content="'.$news['new_title'].'" />
    	                         <meta property="og:image" content="http://fonecare.vn/pictures/news/'.$news['new_picture'].'" />
                            	 <meta property="og:type" content="news" />
                            	 <meta property="og:url" content="http://fonecare.vn/'.$link.'" />
                            	 <meta property="og:title" content="'.$news['new_title'].'" />
                            	 <meta property="fb:app_id" content="524593015322765" />
                            	 <meta property="fb:admins" content="100000472324543" />';
        }else {
            switch($seo_type) {
                case 'news': 
                    $name = 'Tin tức';
                    $des = 'Fone care - tin tức';
                    $key = 'tin tức, tin fonecare, tin công ty fonecare';
                    $rewrite = 'tin-tuc';
                    break;
                case 'recruitment': 
                    $name = 'Tuyển dụng';
                    $des = 'Fone care - tuyển dung, cơ hội việc làm tại fonecare';
                    $key = 'tuyển dụng, tuyển dụng fonecare, tuyển dụng công ty fonecare';
                    $rewrite = 'tuyen-dung';
                    break;
                case 'promotion': 
                    $name = 'Khuyến mại';
                    $des = 'Fone care - khuyến mại, khuyến mại sản phẩm từ fonecare';
                    $key = 'khuyến mại, khuyến mại fonecare, khuyến mại công ty fonecare';
                    $rewrite = 'khuyen-mai';
                    break;
                case 'technology': 
                    $name = 'Góc kỹ thuật';
                    $des = 'Fone care - góc kỹ thuật, chia sẻ kỹ thuật từ fonecare';
                    $key = 'góc kỹ thuật, góc kỹ thuật fonecare, kỹ thuật điện thoại';
                    $rewrite = 'goc-ky-thuat';
                    break;
                default:
                    $name = 'Tin tức';
                    $des = 'Fone care - tin tức';
                    $key = 'tin tức, tin fonecare, tin công ty fonecare';
                    $rewrite = 'tin-tuc';
                    break;
            }
            $this->seo_string = '<title>Fone Care - '.$name.'</title>
                                 <meta name="description" content="'.$des.'" />
                                 <meta name="keywords" content="'.$key.'" />
                                 <meta property="og:site_name" content="'.$name.'" />
    	                         <meta property="og:image" content="http://fonecare.vn/template/default/themes/img/logo.png" />
                            	 <meta property="og:type" content="news" />
                            	 <meta property="og:url" content="http://fonecare.vn/'.$rewrite.'" />
                            	 <meta property="og:title" content="'.$name.'" />
                            	 <meta property="fb:app_id" content="524593015322765" />
                            	 <meta property="fb:admins" content="100000472324543" />';   
        }
    }
    
    public function GetListNews($iType = 'news',$iCat = 0) {
        if($iCat != 0) {
            $sql = 'AND cat_id = '.$iCat;
        }else {
            $sql = '';
        }
        $db_query = new db_query('SELECT new_id,new_title,new_cat_id,cat_type,cat_name,new_date,new_hot,new_active,
                                  new_summary,new_picture,new_detail,new_relate,new_tags,new_type
                                  FROM news
                                  LEFT JOIN categories_multi ON cat_id = new_cat_id
                                  WHERE 1 AND new_active = 1 AND cat_type = "'.$iType.'" '.$sql.'
                                  ORDER BY new_date DESC
                                  LIMIT 12');
        $list_news = array();
        while($row = mysql_fetch_assoc($db_query->result)) {
            $news = array();
            if($iType == 'recruitment') {
                $news['link'] = generate_recruitment_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
            }
            elseif($iType == 'promotion') {
                $news['link'] = generate_promotion_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
            }
            elseif($iType == 'technology') {
                $news['link'] = generate_technology_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
            }
            else {
                $news['link'] = generate_news_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title'],'cat_name'=>$row['cat_name']));
            }
            $news['title'] = $row['new_title'];
            $news['picture'] = PATH_PICTURE_NEWS.$row['new_picture'];
            $news['summary'] = $row['new_summary'];
            $news['date'] = date('h:i a d-m-Y',$row['new_date']);
            $news['detail'] = $row['new_detail'];
            $news['relate'] = $row['new_relate'];
            $news['tags'] = $row['new_tags'];
            $list_news[] = $news;
        }unset($db_query);
        
        $html = NewsLayout::list_news(array('news'=>$list_news));
        return $html;
    }
    
    public function GetDetailNews($iNews,$iType) {
        $db_query = new db_query('SELECT new_id,new_title,new_cat_id,cat_id,cat_name,new_date,new_hot,new_active,
                                  new_summary,new_picture,new_detail,new_relate,new_tags
                                  FROM news
                                  LEFT JOIN categories_multi ON cat_id = new_cat_id
                                  WHERE 1 AND new_active = 1 AND new_id = '.$iNews.'
                                  ORDER BY new_date DESC
                                  LIMIT 1');
        $row = mysql_fetch_assoc($db_query->result);unset($db_query);
        $news = array();
        if($iType == 'recruitment') {
            $news['link'] = generate_recruitment_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
        }
        elseif($iType == 'promotion') {
            $news['link'] = generate_promotion_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
        }
        elseif($iType == 'technology') {
            $news['link'] = generate_technology_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
        }
        else {
            $news['link'] = generate_news_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title'],'cat_name'=>$row['cat_name']));
        }
        $news['title'] = $row['new_title'];
        $news['picture'] = PATH_PICTURE_NEWS.$row['new_picture'];
        $news['summary'] = $row['new_summary'];
        $news['date'] = date('h:i a d-m-Y',$row['new_date']);
        $news['detail'] = $row['new_detail'];
        $news['tags'] = $row['new_tags'];
        
        $relate = array();
        if($row['new_relate']) {
            $arr_relate_id = array();
            $arr_relate_id = explode(',',$row['new_relate']);
            foreach($arr_relate_id as $relate_id) {
                $r = array();
                $db_query_relate = new db_query('SELECT new_id,new_title,new_cat_id,cat_id,cat_name,new_date,new_hot,
                                                 new_active,new_picture,new_relate,new_tags
                                                 FROM news
                                                 LEFT JOIN categories_multi ON cat_id = new_cat_id
                                                 WHERE new_active = 1 AND new_id = '.$relate_id.'
                                                 LIMIT 1');
                $news_relate = mysql_fetch_assoc($db_query_relate->result); unset($db_query_relate);
                $r['link'] = generate_news_detail_url(array('new_id'=>$news_relate['new_id'],'new_title'=>$news_relate['new_title'],'cat_name'=>$news_relate['cat_name']));
                $r['title'] = $news_relate['new_title']; 
                $relate[] = $r;               
            }
        }
        else {
            $db_query_relate = new db_query('SELECT new_id,new_title,new_cat_id,cat_id,cat_name,new_date,new_hot,
                                                 new_active,new_picture,new_relate,new_tags
                                                 FROM news
                                                 LEFT JOIN categories_multi ON cat_id = new_cat_id
                                                 WHERE new_active = 1
                                                 LIMIT 5');
            while($news_relate = mysql_fetch_assoc($db_query_relate->result)) {
                $r = array(); 
                $r['link'] = generate_news_detail_url(array('new_id'=>$news_relate['new_id'],'new_title'=>$news_relate['new_title'],'cat_name'=>$news_relate['cat_name']));
                $r['title'] = $news_relate['new_title'];  
                $relate[] = $r;       
            } 
            unset($db_query_relate);  
        }
        $html = NewsLayout::detail_news(array('news'=>$news,'relate'=>$relate));
        return $html;
    }
    //lấy title và link news cho breakcrumb
    public function GetBreakcrumbNews($iNews,$iType) {
        $db_query = new db_query('SELECT new_id,new_title,new_active,cat_id,cat_name,cat_parent_id
                                  FROM news
                                  LEFT JOIN categories_multi ON cat_id = new_cat_id
                                  WHERE new_active = 1 AND new_id = '.$iNews.'
                                  LIMIT 1');
        $row = mysql_fetch_assoc($db_query->result);unset($db_query);
        $news = array();
        if($iType == 'recruitment') {
            $news['link'] = generate_recruitment_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
        }
        elseif($iType == 'promotion') {
            $news['link'] = generate_promotion_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
        }
        elseif($iType == 'technology') {
            $news['link'] = generate_technology_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
        }
        else {
            $news['link'] = generate_news_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title'],'cat_name'=>$row['cat_name']));
        }
        $news['title'] = $row['new_title'];
        $news['type'] = $iType;
        $news['cat_id'] = $row['cat_id'];
        $news['cat_name'] = $row['cat_name'];
        $html = NewsLayout::breakcrumb_news($news);
        return $html;
    }
    
    public function GetPromotionNews() {
        $db_query = new db_query('SELECT new_id,new_title,new_cat_id,cat_type,new_date,new_hot,new_active,
                                  new_summary,new_picture,new_detail,new_relate,new_tags,new_type
                                  FROM news
                                  LEFT JOIN categories_multi ON cat_id = new_cat_id
                                  WHERE 1 AND new_active = 1 AND cat_type = "promotion"
                                  ORDER BY new_date DESC
                                  LIMIT 6');
        $list_news = array();
        while($row = mysql_fetch_assoc($db_query->result)) {
            $news = array();
            $news['link'] = generate_promotion_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
            $news['title'] = $row['new_title'];
            $list_news[] = $news;
        }unset($db_query);
        
        $html = NewsLayout::promotion_news(array('news'=>$list_news));
        return $html;
    }
}
?>